var urlSunburstTmpl = '@urlSunburst@',
        urlTopPartnersTmpl = '@urlTopPartners@',
        balloonTemplate = "@balloonTemplate@",
        pTradeFlow = "@pTradeFlow@",
        pPartner = "@pPartner@",
        pCommodity = "@pCommodity@",
        pPeriod = "@pPeriod@",
        pPartnerRank = "@pPartnerRank@",
        g01 = "@sunburstId@",
        g02 = "@topNPartnersId@",
        url1 = '@urlTopPartnersClickStart@',
        url2 = '@urlTopPartnersClickVar@',
        url3 = '@urlTopPartnersClickEnd@',
        url4 = '@urlSunburstClickStart@',
        url5 = '@urlSunburstClickVar@',
        url6 = '@urlSunburstClickEnd@';
topNchart = {
    code: g02,
    parameters: [pTradeFlow, pCommodity, pPartnerRank],
    currentUrl: ""
};
d3chart = {
    code: g01,
    graphType: "gt1",
    skipUpdate: false,
    urlPostfix: "?" + g01,
    balloonMaxWidth: 230,
    parameters: [pTradeFlow, pPartner, pPeriod],
    values: ["", "", ""],
    balloonFunction: function (balloonTemplate, d) {
        var shareOfParent = "";
        if (d.parent && d.parent.value) {
            shareOfParent = gw.getFormattedNumber(d.value / d.parent.value * 100, 2, 2) + " %";
        }
        return balloonTemplate
                .replace("@tradeValue", "$ " + gw.getFormattedNumber(d.value, 0, 0))
                .replace("@shareOfParent", shareOfParent).replace("@commodityCode", d.code)
                .replace("@parentCode", d.parent ? d.parent.code : d.code)
                .replace("@commodityName", d.name);
    },
    currentUrl: "",
    onClick: function (urlCode) {
        var url = gw.getUrlInclude(url4, url5, url6, urlCode)
                .replace("[" + pPartnerRank + "]", gw.getSelectValue(pPartnerRank));
        if (document.location.pathname !== url) {
            document.location.pathname = url;
        }
    }
};

function onFilterChanged() {
    refreshSunburst();
    refreshTopNPartners();
}

function refreshTopNPartners() {
    var urlTopPartners = urlTopPartnersTmpl;
    for (var i = 0; i < topNchart.parameters.length; i++) {
        var v = gw.getSelectValue(topNchart.parameters[i]);
        if (!v)
            return;
        else
            urlTopPartners = urlTopPartners.replace("[" + topNchart.parameters[i] + "]", v);
    }
    if (urlTopPartners === topNchart.currentUrl)
        return;
    topNchart.currentUrl = urlTopPartners;
    gw.loadChart($("#" + topNchart.code), urlTopPartners);
}

function refreshSunburst() {
    if (gw.isIE8)
        return;
    var urlSunburst = urlSunburstTmpl;
    for (var i = 0; i < d3chart.parameters.length; i++) {
        var v = gw.getSelectValue(d3chart.parameters[i]);
        if (!v)
            return;
        else
            urlSunburst = urlSunburst.replace("[" + d3chart.parameters[i] + "]", v);
    }
    if (urlSunburst === d3chart.currentUrl)
        return;
    d3chart.currentUrl = urlSunburst;
    gwSunburst.refreshSunBurstGraph(d3chart.code, urlSunburst, d3chart);
}

function onTopNClick(urlCode) {
    var url = gw.getUrlInclude(url1, url2, url3, urlCode)
            .replace("[" + pPartnerRank + "]", gw.getSelectValue(pPartnerRank));
    if (document.location.pathname !== url) {
        document.location.pathname = url;
    }
}